package yt.kpool.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.PopupMenu;

import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import java.math.BigDecimal;
import java.util.List;
import yt.kpool.R;

import yt.kpool.activity.home.CalculatorAc;
import yt.kpool.activity.home.MainstreamCurrencyDetaiAc;
import yt.kpool.activity.miner.AddMinerRemarkActivity;
import yt.kpool.activity.web.ProfitWebAc;
import yt.kpool.bean.PoolCoinBean;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.Util;
import yt.kpool.util.imageUtil.GlideUtil;

public class PoolCoinAdapter extends BaseQuickAdapter<PoolCoinBean.DataBean.CoinListBean, BaseViewHolder> {
    private Context context;
    private Double Exchange;
    private int kind,type;
    private double flash_p=0.00;//电费
    public PoolCoinAdapter(Context context,@Nullable List<PoolCoinBean.DataBean.CoinListBean> data) {
        super(R.layout.adapter_poolcoin, data);
        this.context=context;

    }

    @Override
    protected void convert(final BaseViewHolder helper, final PoolCoinBean.DataBean.CoinListBean item) {
        GlideUtil.loadImage(context, NetHelperNew.URL + item.getIcon(), (ImageView) helper.getView(R.id.iv_coin), R.mipmap.ic_launcher, R.mipmap.ic_launcher);
        helper.setText(R.id.tv_name, item.getCode().toUpperCase());
        helper.setText(R.id.tv_Algorithm, item.getAlgorithm()+"");
        helper.setText(R.id.tv_PoolHashrate, item.getPoolHashrate());
        helper.setText(R.id.tv_ThroughputUnit, "/"+item.getThroughputUnit());


        final BigDecimal c1 = new BigDecimal(item.getPrice());
        BigDecimal c2 = new BigDecimal(Exchange);
        String cp1=c1.multiply(c2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()+"";





        BigDecimal b1 = new BigDecimal(item.getThroughput());
        BigDecimal b2 = new BigDecimal(cp1);
        String sp1=b1.multiply(b2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()+"";




        ImageView tv_PriceTrend=helper.getView(R.id.tv_PriceTrend);

        if(item.getPriceTrend()>0)
        {
            tv_PriceTrend.setImageResource(R.mipmap.shangzhang);
        }
        else if(item.getPriceTrend()<0)
        {
            tv_PriceTrend.setImageResource(R.mipmap.xiadie);
        }

        TextView tv_coinkind=helper.getView(R.id.tv_coinkind);




        if(kind==1)//人民币
        {
            helper.setText(R.id.tv_sy,sp1 );
            tv_coinkind.setText("¥");
            helper.setText(R.id.tv_NetworkHashrate, item.getNetworkHashrate()+"/ ¥"+cp1);
        }
        else if (kind==2)//美元
        {
            helper.setText(R.id.tv_sy, Util.Transformation(sp1) );
            tv_coinkind.setText("$");
            helper.setText(R.id.tv_NetworkHashrate, item.getNetworkHashrate()+"/ $"+Util.GetTNumber(item.getPrice()+""));
        }

        final ImageView imageView=helper.getView(R.id.iv_menu);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupMenu(imageView,helper.getAdapterPosition(),item.getCode()+"");
            }
        });
        helper.getConvertView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainstreamCurrencyDetaiAc.start(context,item.getID()+"",item.getCode(),item.getIcon());

            }
        });
//
    }

    /**
     * 设置转换
     * @param Exchange
     */
    public void setExchange(Double Exchange)
    {
        this.Exchange=Exchange;
    }

    /**
     * 设置币种
     * @param kind
     */
    public void setKind(int kind,double flash_p)
    {
        this.kind=kind;
        this.flash_p=flash_p;

    }

    /**
     * 展示的pop
     * @param view
     */
    private void showPopupMenu(View view,final int postion,final String code) {
        // 这里的view代表popupMenu需要依附的view
        PopupMenu popupMenu = new PopupMenu(context, view);
        // 获取布局文件
        popupMenu.getMenuInflater().inflate(R.menu.sample_menu, popupMenu.getMenu());

        popupMenu.show();
        // 通过上面这几行代码，就可以把控件显示出来了
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                // 控件每一个item的点击事件
                if(item.getTitle().equals("计算器"))
                {

                    CalculatorAc.start(context,postion+"");
                }
                else
                {
                    ProfitWebAc.start(context, NetHelperNew.Tutorial + code, code+ "挖矿教程");
                }
                return true;
            }
        });
        popupMenu.setOnDismissListener(new PopupMenu.OnDismissListener() {
            @Override
            public void onDismiss(PopupMenu menu) {
                // 控件消失时的事件
            }
        });

    }

}
